var CELL_SIZE = 32;
var REEL_SIZE = 13;
var REEL_HEIGHT = CELL_SIZE * REEL_SIZE;

var MAX_SPEED = 16;

enchant();

var data = [];
for ( var i = 0; i < 3; i++) {
	data[i] = [];
}

var reels = [];
var checked = true;

var Shadow = Class.create(Sprite, {
	initialize : function(d, w, h) {
		Sprite.call(this, w, h);
		var s = new Surface(w, h);
		var c = s.context;
		var g;
		if (d == Shadow.LEFT) {
			g = c.createLinearGradient(w, 0, 0, 0);
		} else if (d == Shadow.RIGHT) {
			g = c.createLinearGradient(0, 0, w, 0);
		} else if (d == Shadow.UP) {
			g = c.createLinearGradient(0, h, 0, 0);
		} else if (d == Shadow.DOWN) {
			g = c.createLinearGradient(0, 0, 0, h);
		}
		g.addColorStop(0.0, "rgba(0, 0, 0, 1.0)");
		g.addColorStop(1.0, "rgba(0, 0, 0, 0.0)");
		c.fillStyle = g;
		c.fillRect(0, 0, w, h);
		this.image = s;
	}
});
Shadow.LEFT = 1;
Shadow.RIGHT = 2;
Shadow.UP = 3;
Shadow.DOWN = 4;

$(function() {
	$.each(data, function(i, d) {
		d[0] = 0;
		d[1] = 1;
		d[2] = 2;
		d[3] = 3;
		d[4] = 4;
		d[5] = 5;
		d[6] = 6;
		d[7] = 7;
		d[8] = 8;
		d[9] = 10;
		d[10] = 11;
		d[11] = 12;
		d[12] = 13;
	});

	var game = new Game(160, 160);
	game.preload("chara1.gif");
	game.fps = 60;
	game.onload = function() {
		var stage = new Group();

		var background = new Sprite(game.width, game.height);
		background.backgroundColor = "#ffffff";
		stage.addChild(background);

		var g = new Group();
		g.x = game.width / 2 - CELL_SIZE * 3 / 2;
		g.y -= 0;

		for ( var i = 0; i < 3; i++) {
			reels[i] = new Group();
			reels[i].x = CELL_SIZE * i;
			reels[i].y = 0;
			reels[i].stop = true;
			reels[i].speed = 0;
			for ( var j = 0; j < 2; j++) {
				var r = new Group();
				for ( var c = 0; c < REEL_SIZE; c++) {
					var s = new Sprite(32, 32);
					s.image = game.assets["chara1.gif"];
					s.frame = data[i][c];
					s.x = 0;
					s.y = CELL_SIZE * c;
					s.backgroundColor = "#ffffff";
					s.scale(CELL_SIZE / 32);
					r.addChild(s);
				}
				r.x = 0;
				r.y = CELL_SIZE * REEL_SIZE * j;
				reels[i].addChild(r);
			}
			reels[i].addEventListener(Event.ENTER_FRAME, function(e) {
				if (this.stop) {
					this.firstChild.y = Math.floor(this.firstChild.y
							/ CELL_SIZE)
							* CELL_SIZE;
					this.speed = 0;
				} else {
					this.speed = Math.min(MAX_SPEED, this.speed + 0.5);
					this.firstChild.y += this.speed;
				}

				if (this.firstChild.y >= REEL_HEIGHT) {
					this.firstChild.y -= REEL_HEIGHT * 2;
				}

				if (this.firstChild.y <= 0) {
					this.lastChild.y = this.firstChild.y + REEL_HEIGHT;
				} else {
					this.lastChild.y = this.firstChild.y - REEL_HEIGHT;
				}
			});
			g.addChild(reels[i]);
		}

		stage.addChild(g);

		var s = new Surface(game.width * 2, game.height * 1.7 / 5);
		var c = s.context;
		var grad = c.createLinearGradient(0, 0, 0, s.height);
		grad.addColorStop(0.0, "rgba(0, 0, 0, 1.0)");
		grad.addColorStop(0.5, "rgba(0, 0, 0, 1.0)");
		grad.addColorStop(1.0, "rgba(0, 0, 0, 0.0)");
		c.fillStyle = grad;
		c.fillRect(0, 0, s.width, s.height);

		var top = new Sprite(s.width, s.height);
		top.x -= game.width;
		top.y = 0;
		top.image = s;
		g.addChild(top);

		var bottom = new Sprite(s.width, s.height);
		bottom.x -= game.width;
		bottom.y = game.height - bottom.height + 1;
		bottom.image = s;
		bottom.scaleY *= -1;
		g.addChild(bottom);

		g.y += game.height * 1 / 6;

		var startFrame = 0;
		stage.addEventListener(Event.TOUCH_END, function(e) {
			if (game.frame < startFrame + 20) {
				return;
			}

			if (reels[0].stop && reels[1].stop && reels[2].stop) {
				$.each(reels, function(i, r) {
					r.stop = false;
					r.speed = 0.1;
				});
				startFrame = game.frame;
				return;
			} else if (!reels[0].stop) {
				reels[0].stop = true;
			} else if (!reels[1].stop) {
				reels[1].stop = true;
			} else if (!reels[2].stop) {
				reels[2].stop = true;
				checked = false;
			}
		});

		var topPanel = new Group();
		{
			var topBg = new Sprite(game.width, game.height * 2 / 6);
			topBg.backgroundColor = "#666666";
			topPanel.addChild(topBg);
		}
		stage.addChild(topPanel);

		var leftPanel = new Group();
		{
			var leftBg = new Sprite(30, game.height * 4 / 6 + 5);
			var ls = new Surface(leftBg.width, leftBg.height);
			var lc = ls.context;
			var lg = lc.createLinearGradient(0, 0, 0, leftBg.height);
			lg.addColorStop(0.0, "#440000");
			lg.addColorStop(0.5, "#660000");
			lg.addColorStop(1.0, "#440000");
			lc.fillStyle = lg;
			lc.fillRect(0, 0, leftBg.width, leftBg.height);
			leftBg.image = ls;
			leftPanel.addChild(leftBg);

			var lsdw = new Shadow(Shadow.RIGHT, 2, game.height * 4 / 6 + 5);
			lsdw.x = 30;
			leftPanel.addChild(lsdw);
		}
		leftPanel.y = game.height * 2 / 6;
		stage.addChild(leftPanel);

		var rightPanel = new Group();
		{
			var rightBg = new Sprite(30, game.height * 4 / 6 + 5);
			var rs = new Surface(rightBg.width, rightBg.height);
			var rc = rs.context;
			var rg = rc.createLinearGradient(0, 0, 0, rightBg.height);
			rg.addColorStop(0.0, "#440000");
			rg.addColorStop(0.5, "#660000");
			rg.addColorStop(1.0, "#440000");
			rc.fillStyle = rg;
			rc.fillRect(0, 0, rightBg.width, rightBg.height);
			rightBg.image = rs;
			rightBg.x = 2;
			rightPanel.addChild(rightBg);

			var rsdw = new Shadow(Shadow.LEFT, 2, game.height * 4 / 6 + 5);
			rsdw.x = 0;
			rightPanel.addChild(rsdw);
		}
		rightPanel.x = game.width - rightBg.width;
		rightPanel.y = game.height * 2 / 6;
		stage.addChild(rightPanel);

		stage.addEventListener(Event.ENTER_FRAME, function(e) {
			if (reels[0].speed == 0 && reels[1].speed == 0
					&& reels[2].speed == 0 && !checked) {
				check();
			}
		});

		game.rootScene.addChild(stage);
	};
	game.start();
});

function check() {
	checked = true;

	var stopData = [];
	for ( var i = 0; i < 3; i++) {
		stopData[i] = [];
	}
	$.each(reels, function(i, r) {
		var p0 = (REEL_SIZE - ((r.firstChild.y / CELL_SIZE) % REEL_SIZE))
				% REEL_SIZE;
		stopData[i][0] = data[i][(p0 + 1) % REEL_SIZE];
		stopData[i][1] = data[i][(p0 + 2) % REEL_SIZE];
		stopData[i][2] = data[i][(p0 + 3) % REEL_SIZE];
	});

	if (stopData[0][1] == stopData[1][1] && stopData[1][1] == stopData[2][1]) {
		alert("あたった！");
	}
}
